﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.CBO.SCM.Warehouse;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFSoft.UBF.Business;

namespace UFIDA.U9.Cust.YYD.PlugBE.MOP
{
    public class BeforeInserting : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.MO.MO.MO;
            if (holder == null)
                return;
            #endregion
            if (holder.Org.Code == "1010")//深圳
            {
                //using (ISession session = Session.Open())
                //{
                holder.SCVWh = holder.ItemMaster.InventoryInfo.Warehouse;
                foreach (var line in holder.MOPickLists)
                {
                    line.SupplyWh = line.ItemMaster.InventoryInfo.Warehouse;
                }
                //    session.Commit();
                //}

            }

            if (holder.Org.Code == "1020")//安徽
            {

                Warehouse wh = Warehouse.Finder.Find($"Code = '{holder.ItemMaster.DescFlexField.PrivateDescSeg8}' and Org = {holder.Org.ID}");
                if (wh != null)
                {
                    //using (ISession session = Session.Open())
                    //{
                    holder.SCVWh = wh;
                    foreach (var line in holder.MOPickLists)
                    {
                        Warehouse wh2 = Warehouse.Finder.Find($"Code = '{line.ItemMaster.DescFlexField.PrivateDescSeg8}' and Org = {holder.Org.ID}");

                        line.SupplyWh = wh2;
                    }
                    //    session.Commit();
                    //}

                }

            }
        }
    }
}
